package com.daisuxia.web.synchronization;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.daisuxia.back.dao.ILocalDataDao;
import com.daisuxia.config.PayContents;
import com.daisuxia.constant.Constant;
import com.daisuxia.web.pojo.AuditCenter;
import com.daisuxia.web.pojo.CreditLoanPay;
import com.daisuxia.web.pojo.CreditLoanPayDetail;
import com.daisuxia.web.pojo.CreditLoanPaySum;
import com.daisuxia.web.util.DateUtil;
import com.daisuxia.web.util.HttpUtil;
import com.daisuxia.web.util.encrypt.AESUtil;
import com.daisuxia.web.util.encrypt.MD5coding;

public class syncUtils {
	
	private static Logger loger = Logger.getLogger(syncUtils.class);
	
	/**
	 * 检验是否存在减免状态并做出相应减免
	 */
	public static void checkReduction(HashMap<String,Object> repaymentMap, ILocalDataDao localDataDao){
		String payId = String.valueOf(repaymentMap.get("id"));
		int receivableMoney = Integer.parseInt(String.valueOf(repaymentMap.get("repayment_amount")));//应还金额
		int realMoney= Integer.parseInt(String.valueOf(repaymentMap.get("repaymented_amount")));//已还金额
		
		try {
			loger.info("start-reduction-payId:"+payId);
			AuditCenter auditCenter = localDataDao.getAuditCenterByPayId(payId);
			
			if(null!=auditCenter && "3".equals(auditCenter.getType()) && ("2".equals(auditCenter.getStatus()) || "5".equals(auditCenter.getStatus()))){
				int reductionMoney = auditCenter.getReductionMoney().intValue()*100;//减免金额
				if(realMoney >= receivableMoney-reductionMoney && realMoney < receivableMoney){
					reductionMoney = receivableMoney-realMoney;
					
					String sign = MD5coding.MD5(AESUtil.encrypt(auditCenter.getLoanUserId()+auditCenter.getPayId()+reductionMoney+auditCenter.getId(),PayContents.XJX_WITHHOLDING_NOTIFY_KEY));
                    //2、发起请求
                    String withholdPostUrl=PayContents.XJX_JIANMIAN_URL+"/"+auditCenter.getLoanUserId()+"/"+auditCenter.getPayId()+"/"+reductionMoney+"/"+auditCenter.getId()+"/"+sign;

                    String xjxWithholdingStr = HttpUtil.getHttpMess(withholdPostUrl, "", "POST", "UTF-8");
					
                   loger.info(xjxWithholdingStr);
                    
                   loger.info("payId:"+payId+"需要减免的金额："+reductionMoney);
				}else if(realMoney == receivableMoney){
					 loger.info("payId:"+payId+"钱已还完，无法减免");
				}else{
					 loger.info("未到减免资格，无法减免");
				}
				loger.info("end-reduction-payId:"+payId+"redcutionMoney="+reductionMoney);
			}
			
		}catch (Exception e){
			loger.error("payId:"+payId,e);
		}
	}
	/**
	 * 减免罚息    如果还款类型为6，还款状态2.则更新还款表的 减免滞纳金金额
	 * @param repayDetail 从库还款详情
	 * @param id 还款id
	 */
	public static void ReductionMoney(HashMap<String, Object> repayDetail,String id, ILocalDataDao localDataDao) {
		int repaymentType = Integer.valueOf(String.valueOf(repayDetail.get("repayment_type")));
		int status = Integer.valueOf(String.valueOf(repayDetail.get("status")));
		if(repaymentType==6 && status==2){
			CreditLoanPay creditLoanPay = new CreditLoanPay();
			int reductionMoney =Integer.parseInt(String.valueOf(repayDetail.get("true_repayment_money")));//还款金额，从库中传过来
			creditLoanPay.setId(id);
			creditLoanPay.setReductionMoney(new BigDecimal(reductionMoney / 100.00));
			localDataDao.updateCreditLoanPay(creditLoanPay);
		}
	}

}
